// pages/index/index.js
Page({
	data: {
		carouselItems: [
			{ text: '预约须知', bgColor: 'linear-gradient(135deg, #4A90E2, #357ABD)' },
			{ text: '就诊指南', bgColor: 'linear-gradient(135deg, #4A90E2, #357ABD)' },
			{ text: '停诊通知', bgColor: 'linear-gradient(135deg, #4A90E2, #357ABD)' },
			{ text: '医院导航', bgColor: 'linear-gradient(135deg, #4A90E2, #357ABD)' }
		],
		outpatientServices: [
			{ name: '候诊查询', id: 'waiting_query' },
			{ name: '自助缴费', id: 'self_payment' },
			{ name: '门诊费用', id: 'outpatient_fees' },
			{ name: '在线退款', id: 'online_refund' },
			{ name: '报告查询', id: 'report_query' },
			{ name: '病历查询', id: 'medical_record_query' },
			{ name: '复诊配药', id: 'follow_up' },
			{ name: '体检服务', id: 'physical_exam' },
			{ name: '电子发票', id: 'electronic_invoice' }
		],
		inpatientServices: [
			{ name: '住院充值', id: 'inpatient_recharge' },
			{ name: '住院费用', id: 'inpatient_fees' },
			{ name: '住院日清单', id: 'inpatient_daily' },
			{ name: '病案寄送', id: 'medical_record_delivery' }
		],
		hospitalServices: [
			{ name: '医院介绍', id: 'hospital_intro' },
			{ name: '医院导航', id: 'hospital_nav' },
			{ name: '就医指南', id: 'medical_guide' },
			{ name: '健康百科', id: 'health_encyclopedia' }
		],
		currentIndex: 0,
		currentTab: 'home' // 默认选中首页
	},

	onLoad() {
		// 页面加载时执行
		console.log('页面加载，轮播图数据:', this.data.carouselItems);
		console.log('轮播图数量:', this.data.carouselItems.length);
	},

	onShow() {
		// 页面显示时，确保选中首页
		this.setData({
			currentTab: 'home'
		})
	},

	onSwiperChange(e) {
		this.setData({
			currentIndex: e.detail.current
		})
	},

	onIndicatorClick(e) {
		const index = e.currentTarget.dataset.index
		this.setData({
			currentIndex: index
		})
	},

	onCarouselItemClick(e) {
		const item = e.currentTarget.dataset.item
		const index = e.currentTarget.dataset.index
		
		// 根据轮播图内容进行不同的跳转
		switch (item.text) {
			case '预约须知':
				wx.navigateTo({
					url: '/pages/appointment-notice/appointment-notice'
				});
				break;
			case '就诊指南':
				wx.navigateTo({
					url: '/pages/service-guide/service-guide'
				});
				break;
			case '停诊通知':
				wx.navigateTo({
					url: '/pages/closure-notice/closure-notice'
				});
				break;
			case '医院导航':
				wx.navigateTo({
					url: '/pages/hospital-navigation/hospital-navigation'
				});
				break;
			default:
				wx.showToast({
					title: `点击了${item.text}`,
					icon: 'none'
				});
				console.log('点击轮播图:', item);
		}
	},

	handleServiceClick(e) {
		const service = e.currentTarget.dataset.service
		
		// 根据服务ID进行不同的跳转
		switch (service.id) {
			case 'waiting_query':
				wx.navigateTo({
					url: '/pages/waiting-query/waiting-query'
				});
				break;
			case 'self_payment':
				wx.navigateTo({
					url: '/pages/pending-payment/pending-payment'
				});
				break;
			case 'outpatient_fees':
				wx.navigateTo({
					url: '/pages/outpatient-fees/outpatient-fees'
				});
				break;
			case 'online_refund':
				wx.navigateTo({
					url: '/pages/online-refund/online-refund'
				});
				break;
			case 'report_query':
				wx.navigateTo({
					url: '/pages/report-query/report-query'
				});
				break;
			case 'medical_record_query':
				wx.navigateTo({
					url: '/pages/medical-record-query/medical-record-query'
				});
				break;
			case 'follow_up':
				wx.navigateTo({
					url: '/pages/patient-select-followup/patient-select-followup'
				});
				break;
			case 'physical_exam':
				wx.navigateTo({
					url: '/pages/patient-select-physical/patient-select-physical'
				});
				break;
					case 'electronic_invoice':
				wx.navigateTo({
					url: '/pages/electronic-invoice/electronic-invoice'
				});
				break;
		case 'inpatient_recharge':
			wx.navigateTo({
				url: '/pages/inpatient-select/inpatient-select'
			});
			break;
		case 'inpatient_fees':
			wx.showToast({
				title: '住院费用功能开发中',
				icon: 'none',
				duration: 2000
			});
			break;
		case 'inpatient_daily':
			wx.showToast({
				title: '住院日清单功能开发中',
				icon: 'none',
				duration: 2000
			});
			break;
		case 'medical_record_delivery':
			wx.navigateTo({
				url: '/pages/medical-record-delivery/medical-record-delivery'
			});
			break;
		case 'hospital_intro':
			wx.navigateTo({
				url: '/pages/hospital-guide/hospital-guide'
			});
			break;
		case 'hospital_nav':
			wx.navigateTo({
				url: '/pages/hospital-navigation/hospital-navigation'
			});
			break;
		case 'medical_guide':
			wx.navigateTo({
				url: '/pages/service-guide/service-guide'
			});
			break;
		case 'health_encyclopedia':
			wx.navigateTo({
				url: '/pages/health-encyclopedia/health-encyclopedia'
			});
			break;
		default:
			wx.showToast({
				title: `点击了${service.name}`,
				icon: 'none'
			});
			console.log('点击服务:', service);
	}
},

	goToPersonal() {
		// 检查是否已登录
		const app = getApp()
		const isLoggedIn = app.getLoginStatus()
		
		if (isLoggedIn) {
			// 已登录，直接跳转到个人中心
			wx.redirectTo({
				url: '/pages/2411Ayiliao-pages/profile/profile'
			})
		} else {
			// 未登录，跳转到登录页面
			wx.redirectTo({
				url: '/pages/login/login'
			})
		}
	},

	goToAppointment() {
		wx.navigateTo({
			url: '/pages/patient-select/patient-select'
		})
	},

	goToRecharge() {
		wx.navigateTo({
			url: '/pages/2411Ayiliao-pages/recharge/outpatient'
		})
	},

	switchTab(e) {
		const tab = e.currentTarget.dataset.tab
		this.setData({
			currentTab: tab
		})
		if (tab === 'personal') {
			this.goToPersonal()
		}
	}
}) 